package com.twu.biblioteca;


public class BibliotecaGui {

    private ConsoleHandler console;
    private BibliotecaGuiBooksTab booksTab;


    public BibliotecaGui (){
        console = new ConsoleHandler();
        booksTab = new BibliotecaGuiBooksTab();

    }

    private void writeWelcomeMsg(){
        console.printLineMsg("Welcome to Biblioteca!\n");
    }

    private void printMenuOptions(){
        console.printLineMsg("Menu Options:");
        console.printLineMsg("1- Books");
        console.printLineMsg("2- Membership");
        console.printLineMsg("0- Exit");
    }

    private void writeGoodbyeMsg(){
        console.printLineMsg("Thank you!");
    }

    private void writeMembershipMsg(){
        console.printLineMsg("Please talk to a Librarian. Thank you.");
    }

    private void writeMenuErrorMessage(){
        console.printLineMsg("Select a valid option!!");
    }




    private void handlesMenuSelection(){
        console.printLineMsg("Please select the number from the desired option:");
        int userSelection = validateMenuOption(console.readLineMsg());

        switch (userSelection){
            case 1:
                booksTab.displayBooksTab();
                printMenuOptions();
                handlesMenuSelection();
                break;
            case 2:
                writeMembershipMsg();
                printMenuOptions();
                handlesMenuSelection();
                break;
            case 0:
                writeGoodbyeMsg();
                break;
            case -1:
                writeMenuErrorMessage();
                printMenuOptions();
                handlesMenuSelection();
                break;
        }

    }

    private int validateMenuOption(String consoleInput){
        try {
            int menuIndex = Integer.parseInt(consoleInput);
            if(menuIndex >= 0 && menuIndex <= 2 )
                return menuIndex;
            return -1;
        } catch (NumberFormatException e) {
            return -1;
        }
    }

    public void run(){
        writeWelcomeMsg();
        printMenuOptions();
        handlesMenuSelection();
    }

}
